package controller

import (
	"log/slog"

	"gitee.com/jiebaiyou/formflow/bootstrap"
	"gitee.com/jiebaiyou/formflow/model/questions"
	"gitee.com/jiebaiyou/formflow/pkg/bizcode"
	"gitee.com/jiebaiyou/formflow/pkg/response"
	"github.com/gin-gonic/gin"
)

// QuestionController 处理题目相关请求
type QuestionController struct {
	QuestionService questions.QuestionService
	Env             *bootstrap.Config
}

// GetQuestionTemplates 获取题目模板
func (qc *QuestionController) GetQuestionTemplates(c *gin.Context) {
	// 从路径参数获取类型
	typeStr := c.Param("type")

	// 构建类型过滤
	var types []questions.QuestionType
	if typeStr != "" {
		types = append(types, questions.QuestionType(typeStr))
	}

	// 调用服务
	templates, err := qc.QuestionService.GetQuestionTemplates(c, types)
	if err != nil {
		slog.Error("获取题目模板失败", slog.String("error", err.Error()))
		response.ServerError(c, bizcode.ServerError.String(), err)
		return
	}

	response.OK(c, bizcode.Success.String(), templates)
}

// CreateQuestion 创建题目
func (qc *QuestionController) CreateQuestion(c *gin.Context) {
	// 直接接收请求并解析
	var template *questions.QuestionTemplate
	if err := c.ShouldBindJSON(&template); err != nil {
		response.BadRequest(c, bizcode.InvalidParams.String(), err)
		return
	}

	// 调用Service层，不需要传入questionType参数
	err := qc.QuestionService.CreateQuestion(c, template)
	if err != nil {
		slog.Error("创建题目失败", slog.String("error", err.Error()))
		response.ServerError(c, bizcode.ServerError.String(), err)
		return
	}

	response.OK(c, bizcode.Success.String(), nil)
}

// UpdateQuestion 更新题目
func (qc *QuestionController) UpdateQuestion(c *gin.Context) {
	response.OK(c, bizcode.Success.String(), nil)
}

// DeleteQuestion 删除题目
func (qc *QuestionController) DeleteQuestion(c *gin.Context) {
	response.OK(c, bizcode.Success.String(), nil)
}

// GetQuestionByID 获取单个题目
func (qc *QuestionController) GetQuestionByID(c *gin.Context) {
	response.OK(c, bizcode.Success.String(), nil)
}

// ListQuestions 获取题目列表
func (qc *QuestionController) ListQuestions(c *gin.Context) {
	response.OK(c, bizcode.Success.String(), nil)
}
